package com.atchangsha.system.service.impl;

import com.atchangsha.system.common.utils.Result;
import com.atchangsha.system.constant.AdminConstants;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atchangsha.system.common.utils.PageUtils;
import com.atchangsha.system.common.utils.Query;
import com.atchangsha.system.dao.AdminDao;
import com.atchangsha.system.entity.AdminEntity;
import com.atchangsha.system.service.AdminService;

@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                new QueryWrapper<AdminEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Result login(String username, String password) {
        AdminEntity adminEntity = baseMapper.selectOne(new QueryWrapper<AdminEntity>().eq("username", username));
        if (adminEntity == null){
            //数据库没有该账号
            return Result.fail(AdminConstants.AdminEnum.NOT_FIND);
        }else {
            if (!adminEntity.getPassword().equals(password)){
                //密码错误
                return Result.fail(AdminConstants.AdminEnum.PASSWORD_ERROR);
            }else {
                return Result.ok(AdminConstants.AdminEnum.LOGIN).setResult(adminEntity);
            }
        }
    }
}